package com.hupu.jrs.ui.pmdetail

import android.text.TextUtils

import com.gzsll.hupu.api.game.GameApi
import com.gzsll.hupu.bean.BaseData
import com.gzsll.hupu.bean.PmDetail
import com.gzsll.hupu.bean.PmDetailData
import com.gzsll.hupu.bean.PmDetailResult
import com.gzsll.hupu.bean.PmSettingData
import com.gzsll.hupu.bean.SendPm
import com.gzsll.hupu.bean.SendPmData
import com.gzsll.hupu.components.storage.UserStorage
import com.gzsll.hupu.injector.PerActivity
import com.gzsll.hupu.util.ToastUtil

import java.util.ArrayList

import javax.inject.Inject

import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Action1
import rx.schedulers.Schedulers

/**
 * Created by sll on 2016/5/6.
 */
@PerActivity
class PmDetailPresenter @Inject
constructor(private val uid: String, private val mGameApi: GameApi, private val mUserStorage: UserStorage) : PmDetailContract.Presenter {

    private var mPmDetailView: PmDetailContract.View? = null
    private var lastMid = ""
    private val mPmDetails = ArrayList<PmDetail>()

    private var mSubscription: Subscription? = null
    private var isBlock: Boolean = false

    fun attachView(view: PmDetailContract.View) {
        mPmDetailView = view
        mGameApi.queryPmSetting(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> { pmSettingData ->
                    if (pmSettingData != null) {
                        isBlock = pmSettingData.result.is_block === 1
                        mPmDetailView!!.isBlock(isBlock)
                    }
                }, Action1<Throwable> { })
    }

    override fun onPmDetailReceive() {
        mPmDetailView!!.showLoading()
        loadPmDetail()
    }

    private fun loadPmDetail() {
        mSubscription = mGameApi.queryPmDetail(lastMid, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> { pmDetailData ->
                    mPmDetailView!!.hideLoading()
                    if (pmDetailData != null) {
                        val result = pmDetailData.result
                        if (!result.data.isEmpty()) {
                            lastMid = result.data.get(0).pmid
                            mPmDetails.addAll(0, result.data)
                            mPmDetailView!!.renderPmDetailList(mPmDetails)
                            mPmDetailView!!.scrollTo(result.data.size() - 1)
                            mPmDetailView!!.onRefreshCompleted()
                        } else {
                            if (mPmDetails.isEmpty()) {
                                mPmDetailView!!.onEmpty()
                            } else {
                                ToastUtil.showToast("没有更多了")
                                mPmDetailView!!.onRefreshCompleted()
                            }
                        }
                    }
                }, Action1<Throwable> {
                    if (mPmDetails.isEmpty()) {
                        mPmDetailView!!.onError()
                    } else {
                        ToastUtil.showToast("数据加载失败，请检查网络后重试")
                        mPmDetailView!!.hideLoading()
                    }
                })
    }

    override fun onLoadMore() {
        loadPmDetail()
    }

    override fun onReload() {
        onPmDetailReceive()
    }

    override fun send(content: String) {
        if (TextUtils.isEmpty(content)) {
            ToastUtil.showToast("内容不能为空")
            return
        }
        mGameApi.pm(content, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> { sendPmData ->
                    if (sendPmData != null) {
                        val pm = sendPmData.result
                        if (pm.code.equals("0")) {
                            val detail = PmDetail()
                            detail.puid = mUserStorage.getUid()
                            detail.header = mUserStorage.getUser().getIcon()
                            detail.content = pm.content
                            detail.pmid = pm.pmid
                            detail.create_time = pm.create_time
                            mPmDetails.add(detail)
                            mPmDetailView!!.renderPmDetailList(mPmDetails)
                            mPmDetailView!!.scrollTo(mPmDetails.size - 1)
                            mPmDetailView!!.onRefreshCompleted()
                            ToastUtil.showToast("发送成功")
                            mPmDetailView!!.cleanEditText()
                        } else {
                            ToastUtil.showToast(sendPmData.result.desc)
                        }
                    }
                }, Action1<Throwable> { ToastUtil.showToast("发送失败，请检查您的网络后重试") })
    }

    override fun clear() {
        mGameApi.clearPm(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> { ToastUtil.showToast("清空记录成功") }, Action1<Throwable> { ToastUtil.showToast("清空记录失败，请检查网络后重试") })
    }

    override fun block() {
        mGameApi.blockPm(uid, if (isBlock) 0 else 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Action1<Any> {
                    ToastUtil.showToast(if (isBlock) "取消屏蔽成功" else "屏蔽成功")
                    isBlock = !isBlock
                    mPmDetailView!!.isBlock(isBlock)
                }, Action1<Throwable> { ToastUtil.showToast(if (isBlock) "取消屏蔽失败，请检查网络后重试" else "屏蔽失败，请检查网络后重试") })
    }

    fun detachView() {
        if (mSubscription != null && !mSubscription!!.isUnsubscribed) {
            mSubscription!!.unsubscribe()
        }
        mPmDetailView = null
    }
}
